<template>
  <div>
        <el-form label-width="85px" :model="form" label-position="right" ref="formRef" :rules="formRules">
          <el-form-item label="编号" prop="number">
            <el-input v-model="form.number"></el-input>
          </el-form-item>
          <el-form-item label="部门名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="上级职位" prop="parentId">
            <el-tree-select v-model="form.parentId" :data="parentData" :render-after-expand="false"
              check-strictly="true" />
          </el-form-item>
          <el-form-item label="级别" prop="itemIndex">
            <el-input-number v-model="form.itemIndex" :min="0" :max="99" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-switch v-model="form.status" size="large" inline-prompt :active-icon="Check" :inactive-icon="Close"
              active-value="normal" inactive-value="disable" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark"></el-input>
          </el-form-item>
        </el-form>
        <div class="button-position"> 
          <el-button @click="updateVisible">取 消</el-button>
          <el-button type="primary" @click="updataPosition(formRef)">确 定</el-button>
        </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { ElNotification } from "element-plus";
import { Check, Close } from '@element-plus/icons-vue';
import Api from '@/api/model/user'
const emit = defineEmits(['updatetable'])

const props = defineProps({
  paramsData: {
    type: Object,
    defalut: {},
  },
});
onMounted(() => {
  getParent();
  getData();
});

// 查看用户数据的对象
const form = ref({});
const formRef = ref();
const formRules = reactive({
  number: [{ required: true, message: "请输入编号", trigger: "blur" }],
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
});

const updateVisible = (val) => {
  emit('updatetable')
};

const parentData = ref([]);
const getParent = async () => {
  let params = {
    fileName: "PositionTreeReport",
  };
  let res = await Api.position.positionTree.post(params);
  parentData.value = res.reportData;
};

const getData = async () => {
  let res = await Api.position.detail.post(
    props.paramsData
  );
  if (res.code == 200) {
    form.value = res.data;
  }
};

const updataPosition = async (formRef) => {
  await formRef.validate((valid, fields) => {
    if (valid) {
      updataPositionData();
    }
  });
};

const updataPositionData = async () => {
  var params = {
    bean: form.value,
  };
  var res = await Api.position.update.post(params);
  if (res.code == 200) {
    ElNotification.success({
      title: "",
      message: "修改成功！",
      duration: 2000,
    });
    emit("updatetable",true);
  }
};
</script>

<style lang="less" scoped></style>

